package cn.micro.system.manage.controller;


import cn.micro.system.manage.annotate.UniquePosition;
import cn.micro.system.manage.common.log.annotation.Log;
import cn.micro.system.manage.common.log.enums.BusinessType;
import cn.micro.system.manage.common.result.Result;
import cn.micro.system.manage.common.vaildate.ValidateGroup;
import cn.micro.system.manage.entity.SysDimPosition;
import cn.micro.system.manage.entity.dto.SysDimPositionSearch;
import cn.micro.system.manage.service.SysDimPositionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 岗位信息表 前端控制器
 * </p>
 *
 * @author wr
 * @since 2023-12-25
 */
@RestController
@RequestMapping("/manage/sysDimPosition")
@RequiredArgsConstructor
public class SysDimPositionController {

    private final SysDimPositionService sysDimPositionService;

    @GetMapping("/{page}/{size}")
    @PreAuthorize("hasAuthority('bnt.sysPost.list')")
    public Object list(@PathVariable int page, @PathVariable int size, SysDimPositionSearch search){
        return Result.success(new PageInfo<>(sysDimPositionService.listByPage(page, size, search)));
    }

    @GetMapping("detail/{id}")
    @PreAuthorize("hasAuthority('bnt.sysPost.list')")
    public Object detail(@PathVariable Long id){
        return Result.success(sysDimPositionService.getDetail(id));
    }

    @PostMapping("save")
    @Log(title = "岗位管理--新增岗位信息", businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.sysDept.add')")
    public Object save(@Validated(value = ValidateGroup.add.class) @UniquePosition @RequestBody SysDimPosition sysDimPosition){
        if (!sysDimPositionService.save(sysDimPosition)){
            return Result.fail();
        }
        return Result.success();
    }

    @PutMapping("edit")
    @Log(title = "岗位管理--修改岗位信息", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.sysDept.update')")
    public Object edit(@Validated(value = ValidateGroup.edit.class) @UniquePosition @RequestBody SysDimPosition sysDimPosition){
        if (!sysDimPositionService.updateById(sysDimPosition)){
            return Result.fail();
        }
        return Result.success();
    }

    @DeleteMapping("remove/{id}")
    @Log(title = "岗位管理--删除岗位信息", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysDept.remove')")
    public Object remove(@PathVariable Long id){
        if (!sysDimPositionService.removeById(id)){
            return Result.fail();
        }
        return Result.success();
    }

    @GetMapping("listByDeptId/{deptId}")
    public Object listByDeptId(@PathVariable String deptId){
        LambdaQueryWrapper<SysDimPosition> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDimPosition::getDeptId, deptId).orderByAsc(SysDimPosition::getCreateTime);
        return Result.success(sysDimPositionService.list(queryWrapper));
    }
}
